<template>
  <div>
    <div class="search">
      <input type="text" placeholder="输入城市或拼音" v-model="keyword">
    </div>
    <div class="wrapper" ref="wrapper" v-show="keyword">
      <ul class="result">
        <li v-for="item of result" :key="item.id">{{item.name}}</li>
        <li v-show="!result[0]" class="none">搜索不到该城市</li>
      </ul>
    </div>
  </div>
</template>
<script>
import BScroll from "better-scroll";
export default {
  name: "CitySearch",
  props: ["cities"],
  data() {
    return {
      keyword: "",
      result: []
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.scroll = new BScroll(this.$refs.wrapper, {});
    });
  },
  watch: {
    keyword() {
      if (!this.keyword) {
        this.result = [];
      } else {
        var result = [];
        for (let k in this.cities) {
          this.cities[k].forEach(ele => {
            if (
              ele.name.startsWith(this.keyword) ||
              ele.spell.startsWith(this.keyword)
            ) {
              result.push(ele);
            }
          });
          this.result = result;
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.wrapper {
  width: 100%;
  height: 1135px;
  overflow: hidden;
  position: fixed;
  top: 200px;
  left: 0;
  z-index: 999;
  background: #fff;
}
.search {
  position: fixed;
  width: 100%;
  box-sizing: border-box;
  top: 111px;
  left: 0;
  padding: 10px 25px;
  background: rgb(2, 188, 213);
  z-index: 999;
}
input {
  width: 100%;
  height: 70px;
  border-radius: 20px;
  padding: 0 30px;
  box-sizing: border-box;
  outline: none;
}
li {
  line-height: 70px;
  border-bottom: 1px solid #eee;
  padding-left: 40px;
}
.none {
  text-align: center;
  color: #aaa;
}
</style>
